<%--
  - Copyright 2011 Angel Sanadinov
  -
  - This file is part of VBox WMI.
  -
  - VBox WMI is free software: you can redistribute it and/or modify
  - it under the terms of the GNU General Public License as published by
  - the Free Software Foundation, either version 3 of the License, or
  - (at your option) any later version.
  -
  - VBox WMI is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU General Public License for more details.
  -
  - You should have received a copy of the GNU General Public License
  - along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
  --%>

<%@page import="Beans.PermissionsBean"%>
<%@page import="Utilities.Constants"%>
<%@page import="Utilities.Constants.ResourceRequestType"%>
<%@page import="Beans.Responses.DataRetrievalComponents.ServerDataComponent"%>
<%@page import="Beans.Responses.DataRetrievalResponse"%>
<%@page import="Utilities.UIFunctions"%>
<%@page import="Utilities.ParameterNames"%>
<%@page import="Utilities.Constants.SendingRequestResult"%>
<%@page import="Utilities.ParameterNames.Service"%>
<%@page import="Managers.Utilities.User"%>
<%@page import="Beans.Requests.DataRetrieval.ServersDataRetrievalRequest"%>
<%@page import="Utilities.Functions"%>
<%@page contentType="text/html" pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%@taglib uri="/WEB-INF/tlds/pagePrintingTags.tld" prefix="p"%>
<%
//retrieves user data from the session
    User userSessionData = (User)session.getAttribute(ParameterNames.SESSION_USER_OBJECT);

    //data retrieval request and response objects
    ServersDataRetrievalRequest dataRequest = null;
    DataRetrievalResponse<ServerDataComponent> data = null;

    //attempts to retrieve the ID of a selected server, if available
    int changeServerId = Constants.INVALID_SERVER_ID;
    try
    {
        changeServerId = Integer.parseInt(request.getParameter("change"));
    }
    catch(NumberFormatException e){}

    //attempts to retrieve the ID of a server to be retrieved
    int viewServerId = Constants.INVALID_SERVER_ID;
    try
    {
        viewServerId = Integer.parseInt(request.getParameter("id"));
    }
    catch(NumberFormatException e){}

    //based on the supplied data, builds the request object
    if(Functions.isServerIdValid(viewServerId)) //a server is to be retrieved
        dataRequest = new ServersDataRetrievalRequest(userSessionData.getUserData().getUserId(), viewServerId);
    else if(Functions.isServerIdValid(changeServerId)) //a new server is to be selected
    {
        PermissionsBean serverPermissions = userSessionData.getPermissions(changeServerId);
        if(userSessionData.getUserData().getAccountLevel() >= Constants.DB_USER_ACCOUNT_LEVEL_OWNER
           || (serverPermissions != null && serverPermissions.isValid()))
        {
            userSessionData.setCurrentServerId(changeServerId);
        }
        else
            ;

        response.sendRedirect("servers.jsp");
        return;
    }
    else //all available servers are to be retrieved
        dataRequest = new ServersDataRetrievalRequest(userSessionData.getUserData().getUserId());

    //attempts to submit the request and retreives the response
    if(UIFunctions.sendServiceRequest(request, response, dataRequest, Service.DATA_RETRIEVAL) == SendingRequestResult.OK)
        data = (DataRetrievalResponse<ServerDataComponent>)UIFunctions.retrieveServiceResponse(request, Service.DATA_RETRIEVAL);
    else
        ;
%>
<p:printPageHeader pageName="Servers Page" pageId="pageServers" userSessionData="<%= userSessionData %>"/>
        <p:printServers data="<%= data %>" 
                        isOwner="<%= userSessionData.getUserData().getAccountLevel() >= Constants.DB_USER_ACCOUNT_LEVEL_OWNER %>" />
<p:printPageFooter/>